home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / vgl20.zip / VGLFLI.C < prev    next >
C/C++ Source or Header  |  1993-05-18  |  10KB  |  255 lines

  1. /*****************************************************************************
  2.  VGLFLI.C
  3.  
  4.  Simple (and slow) C routine to play a 320x200 .FLI file.  You specify the
  5.  filename of the flick to be played, a pointer to a screen buffer, and a
  6.  speed (in clock ticks).  If the screen buffer you specify is VIDMEM, you may
  7.  get some shearing but it'll probably be faster.  Anything other than VIDMEM
  8.  means vglUpdateW is called to update video memory with each frame.  No
  9.  flicker, but slower (usually).
  10.  
  11.  This is an example of lousy coding.  It is slow.  I wrote it a long time ago
  12.  and just found it again the other day.  I'm throwing it into the soup
  13.  because it may be of some use to someone.
  14.  
  15.  This will play the .FLI in a loop until any key is pressed.
  16.  
  17.  Mark
  18.  morley@camosun.bc.ca
  19. *****************************************************************************/
  20.  
  21. #include <stdio.h>
  22. #include <dos.h>
  23. #include <time.h>
  24.  
  25. #define BUFSIZE  8192                   /* Maximum buffer to try and        */
  26.                                         /* allocate for file reading.       */
  27.  
  28. static char far  Pal[768];              /* Our palette buffer               */
  29. static char*     VBuf;                  /* Will point to an I/O buffer      */
  30.  
  31. #define FLI_COLOR       11              /* Types of FLI chunks              */
  32. #define FLI_LC          12
  33. #define FLI_BLACK       13
  34. #define FLI_BRUN        15
  35. #define FLI_COPY        16
  36.  
  37. typedef struct                          /* The structure of a .FLI header   */
  38. {
  39.    unsigned long size;
  40.    unsigned int  magic;
  41.    unsigned int  frames;
  42.    unsigned int  width;
  43.    unsigned int  height;
  44.    unsigned int  bits;
  45.    unsigned int  flags;
  46.    unsigned int  speed;
  47.    unsigned long next;
  48.    unsigned long frit;
  49.    unsigned char expand[102];
  50. } FLIHDR;
  51.  
  52. typedef struct                          /* The structure of a frame header  */
  53. {
  54.    unsigned long size;
  55.    unsigned int  magic;
  56.    unsigned int  chunks;
  57.    unsigned char expand[8];
  58. } FRAMEHDR;
  59.  
  60. /*****************************************************************************
  61.  A single routine to open, play, and close a .FLI file.  Slow and simple.
  62. *****************************************************************************/
  63. vglPlayFLI( char* file, char far* video, int speed )
  64. {
  65.    register unsigned x;
  66.    register unsigned y;
  67.    char              buf[81];
  68.    FILE*             fp;
  69.    FLIHDR            hdr;
  70.    FRAMEHDR          frm;
  71.    unsigned long     size;
  72.    unsigned int      type;
  73.    unsigned int      pakets;
  74.    unsigned char     skip;
  75.    unsigned int      cnt;
  76.    unsigned int      lines;
  77.    unsigned int      change;
  78.    signed char       s;
  79.    unsigned char     v;
  80.    unsigned int      i;
  81.    unsigned int      c;
  82.    unsigned int      f;
  83.    unsigned long     vc;
  84.    long              RewindPos;
  85.    clock_t           tiks;
  86.    char far*         vid;
  87.  
  88.    /* Open up the file */
  89.    fp = fopen( file, "rb" );
  90.    if( !fp )
  91.       return 0;
  92.  
  93.    /* Try and allocate a buffer to help speed up the file I/O */
  94.    if( (VBuf = (char*) malloc( BUFSIZE )) == 0 )
  95.       VBuf = (char*) malloc( BUFSIZE / 2 );
  96.    if( VBuf )
  97.       setvbuf( fp, VBuf, _IOFBF, BUFSIZE );
  98.  
  99.    /* Read the header */
  100.    fread( &hdr, sizeof(FLIHDR), 1, fp );
  101.    f = 0;
  102.  
  103.    while( 1 )
  104.    {
  105.       /* For each frame in the flick we... */
  106.       for( ; f <= hdr.frames; f++ )
  107.       {
  108.          tiks = clock();
  109.  
  110.          /* Read in a frame header */
  111.          fread( &frm, sizeof(FRAMEHDR), 1, fp );
  112.  
  113.          /* For each chunk in a frame we... */
  114.          if( frm.size != 0L )  for( i = 0; i < frm.chunks; i++ )
  115.          {
  116.             fread( &size, 1, sizeof(long), fp );
  117.             type = getw( fp );
  118.  
  119.             /* Process each chunk */
  120.             switch( type )
  121.             {
  122.                case FLI_LC         : lines = getw( fp );
  123.                                      change = getw( fp );
  124.                                      vc = 0;
  125.                                      y = lines;
  126.                                      i = y * 320;
  127.                                      for( c = 0; c < change; c++ )
  128.                                      {
  129.                                         vid = video + i;
  130.                                         i += 320;
  131.                                         pakets = getc( fp );
  132.                                         vc++;
  133.                                         while( pakets-- )
  134.                                         {
  135.                                            skip = getc( fp );
  136.                                            vc++;
  137.                                            vid += skip;
  138.                                            s = getc( fp );
  139.                                            vc++;
  140.                                            if( s > 0 )
  141.                                            {
  142.                                               vc += s;
  143.                                               while( s-- )
  144.                                                  *vid++ = getc( fp );
  145.                                            }
  146.                                            else
  147.                                            {
  148.                                               s = -s;
  149.                                               v = getc( fp );
  150.                                               vc++;
  151.                                               while( s-- )
  152.                                                  *vid++ = v;
  153.                                            }
  154.                                         }
  155.                                         y++;
  156.                                      }
  157.                                      if( vc & 1 )
  158.                                         getc( fp );
  159.                                      break;
  160.                case FLI_BRUN       : vc = 0;
  161.                                      vid = video;
  162.                                      for( y = 0; y < 200; y++ )
  163.                                      {
  164.                                         pakets = getc( fp );
  165.                                         while( pakets-- )
  166.                                         {
  167.                                            vc++;
  168.                                            s = getc( fp );
  169.                                            if( s < 0 )
  170.                                            {
  171.                                               s = -s;
  172.                                               vc += s;
  173.                                               while( s-- )
  174.                                                  *vid++ = getc( fp );
  175.                                            }
  176.                                            else
  177.                                            {
  178.                                               v = getc( fp );
  179.                                               vc++;
  180.                                               while( s-- )
  181.                                                  *vid++ = v;
  182.                                            }
  183.                                         }
  184.                                      }
  185.                                      if( vc & 1 )
  186.                                         getc( fp );
  187.                                      break;
  188.                case FLI_COLOR      : vc = 2;
  189.                                      pakets = getw( fp );
  190.                                      c = 0;
  191.                                      while( pakets-- )
  192.                                      {
  193.                                         skip = getc( fp );
  194.                                         vc++;
  195.                                         c += skip * 3;
  196.                                         cnt = 0;
  197.                                         cnt = getc( fp );
  198.                                         vc++;
  199.                                         if( cnt == 0 )
  200.                                            cnt = 256;
  201.                                         while( cnt-- )
  202.                                         {
  203.                                            Pal[c++] = getc( fp );
  204.                                            Pal[c++] = getc( fp );
  205.                                            Pal[c++] = getc( fp );
  206.                                            vc += 3;
  207.                                         }
  208.                                      }
  209.                                      vglSetPal( Pal );
  210.                                      if( vc & 1 )
  211.                                         getc( fp );
  212.                                      break;
  213.                case FLI_COPY       : vid = video;
  214.                                      for( y = 0; y < 200; y++ )
  215.                                         for( x = 0; x < 320; x++ )
  216.                                            *vid++ = getc( fp );
  217.                                      break;
  218.                case FLI_BLACK      : vglClear( 0 );
  219.                                      break;
  220.  
  221.                /* A bad or unknown chuck type (a .FLC perhaps?) */
  222.                default             : fclose( fp );
  223.                                      if( VBuf )
  224.                                         free( VBuf );
  225.                                      return 1;
  226.             }
  227.          }
  228.  
  229.          /* If we're writing to a virtual screen, update the real one! */
  230.          if( video != (char far*) 0xa0000000L )
  231.             vglUpdateW();
  232.  
  233.          /* Check for a keypress */
  234.          if( kbhit() )
  235.          {
  236.             if( getch() == 0 )
  237.                getch();
  238.             fclose( fp );
  239.             if( VBuf )
  240.                free( VBuf );
  241.             return 1;
  242.          }
  243.  
  244.          /* Pause the specified number of clock ticks */
  245.          while( clock() - tiks < speed );
  246.  
  247.          if( f == 0 )
  248.             RewindPos = ftell( fp );
  249.       }
  250.       fseek( fp, RewindPos, 0 );
  251.       f = 1;
  252.    }
  253. }
  254.  
  255.